package Team

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/module/role/team"
	"sanguo/node/node_game/user"
	cs_message "sanguo/protocol/cs/message"
	ss_rpc "sanguo/protocol/ss/rpc"
	"sanguo/rpc/createTeam"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionCreateTeam struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.CreateTeamToC
	role    *role.Role
	errcode cs_message.EnumType
}

func (this *transactionCreateTeam) GetModuleName() string {
	return "Team"
}

func (this *transactionCreateTeam) GetReplyMsg() proto.Message {
	return &cs_message.CreateTeamToC{}
}

func (this *transactionCreateTeam) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionCreateTeam) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionCreateTeam) Begin() {
	this.resp = &cs_message.CreateTeamToC{}

	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	this.role = roleModule.CurrentRole()
	if nil == this.role || !this.role.IsRoleModleData() {
		node_game.Debugln(this.role.GetUserID(), "fail no selected role")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
	}

	if this.role.Team != nil {
		node_game.Debugln(this.role.GetUserID(), this.role.GetID(), "CreateTeam fail : this.role.Team != nil ")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	msg := this.req.GetData().(*cs_message.CreateTeamToS)

	arg := &ss_rpc.CreateTeamReq{
		TeamType:   proto.Int32(msg.GetTeamType()),
		UserID:     proto.String(this.user.GetUserID()),
		Name:       proto.String(this.role.GetName()),
		RoleID:     proto.Uint64(this.role.GetID()),
		Level:      proto.Int32(this.role.GetLev()),
		Portrait:   proto.Int32(this.role.GetPortrait()),
		Face:       proto.Int32(this.role.GetFace()),
		Hair:       proto.Int32(this.role.GetHair()),
		HairColor:  proto.Int32(this.role.GetHairColor()),
		EyeColor:   proto.Int32(this.role.GetEyeColor()),
		Cloth:      proto.Int32(this.role.GetCloth()),
		ClothColor: proto.Int32(this.role.GetClothColor()),
		Weapon1:    proto.Int32(this.role.GetWeapon1()),
		Game:       proto.String(node_game.GetSelfService().ToPeerID().ToString()),
	}

	createTeam.AsynCall(node_game.GetTeamServer().ToPeerID(), arg, transaction.TransTimeoutMs, func(resp *ss_rpc.CreateTeamResp, err error) {
		if !this.user.CheckTrans(this) {
			//trans已经结束
			this.errcode = cs_message.EnumType_RETRY
			this.user.EndTrans(this)
			return
		}
		if nil == err && resp.GetOk() == 1 {
			_team := &team.Team{
				TeamID:     resp.GetTeamID(),
				TeamHeader: true,
			}
			this.role.Team = _team

			node_game.Debugln(this.role.GetUserID(), this.role.GetID(), "CreateTeam Ok")
			this.errcode = cs_message.EnumType_OK
			this.user.EndTrans(this)
		} else {
			node_game.Debugln(this.role.GetUserID(), this.role.GetID(), "CreateTeam fail")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
		}
	})
}

func (this *transactionCreateTeam) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionCreateTeam) Timeout() {
	this.errcode = cs_message.EnumType_RETRY
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.CreateTeamToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionCreateTeam{
			user: user,
			req:  msg,
		}
	})
}
